package com.pandora.framework.util;

import java.sql.DriverManager;
import java.sql.SQLException;
import com.alibaba.druid.pool.DruidDataSource;
import com.pandora.common.util.StringUtils;

/**
 * 数据源配置
 * 
 * @author JingChenglong 2017/12/16 13:28
 *
 */
public class DBConfig {

	private static DruidDataSource dds = null;

	public static void init() {

		dds = new DruidDataSource();
		dds.setDriverClassName(PropertiesUtils.getDb("DRIVERCLASSNAME"));
		dds.setUrl(PropertiesUtils.getDb("URL"));
		dds.setUsername(PropertiesUtils.getDb("USERNAME"));
		dds.setPassword(PropertiesUtils.getDb("PASSWORD"));
		dds.setDefaultAutoCommit(StringUtils.nullToBoolean(PropertiesUtils.getDb("DEFAULTAUTOCOMMIT")));
		dds.setDefaultReadOnly(StringUtils.nullToBoolean(PropertiesUtils.getDb("DEFAULTREADONLY")));
		dds.setMaxActive(StringUtils.nullToIntZero(PropertiesUtils.getDb("MAXACTIVE")));
		dds.setMinIdle(StringUtils.nullToIntZero(PropertiesUtils.getDb("MINIDLE")));
		dds.setMaxWait(StringUtils.nullToLongZero(PropertiesUtils.getDb("MAXWAIT")));
		dds.setTimeBetweenEvictionRunsMillis(
				StringUtils.nullToLongZero(PropertiesUtils.getDb("TIMEBETWEENEVICTIONRUNSMILLIS")));
	}

	public static DruidDataSource getInstance() {
		return dds;
	}

	public static void close() throws SQLException {
		DriverManager.deregisterDriver(DriverManager.getDriver(PropertiesUtils.getDb("URL")));
		dds.close();
		dds = null;
	}
}